//
//  NSHomeCell.m
//  Meboo
//
//  Created by kyo on 2023/12/6.
//

#import "NSHomeCell.h"
#import "FXBlurView.h"
@interface NSHomeCell()
@property (nonatomic , strong) UIButton *newBtn;

@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIImageView *starImageView;

@end

@implementation NSHomeCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Public Metohd
- (void)setIsLocked:(BOOL)isLocked {
    _isLocked = isLocked;
//    self.effectView.hidden = !isLocked;
//    self.starImageView.hidden = !isLocked;
    
    if (isLocked) {
        self.iconImageView.hidden = YES;
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            self.iconImageView.image = [image blurredImageWithRadius:BlurValue iterations:2 tintColor:HEXS(@"000000")];
            self.iconImageView.hidden = NO;
        }];
    } else {
        [self.iconImageView sd_setImageWithURL:UrlWithString(self.imageUrl)];
    }
}

- (void)setIsNew:(BOOL)isNew {
    _isNew = isNew;
    self.newBtn.hidden = !isNew;
}

- (void)setIsVideo:(BOOL)isVideo {
    _isVideo = isVideo;
    self.timeLab.hidden = !isVideo;
}

- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
}

#pragma mark - Action
//- (void)deleteCloth {
//    if (self.deleteBlock) {
//        self.deleteBlock();
//    }
//}

#pragma mark - UI
- (void)setupUI {
    self.contentView.backgroundColor = UIColor.clearColor;
    self.contentView.layer.cornerRadius = MBHeightScale(18);
    self.contentView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.iconImageView addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.iconImageView);
    }];
    
    [self.iconImageView addSubview:self.starImageView];
    [self.starImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.iconImageView);
        make.width.height.mas_equalTo(MBHeightScale(32));
    }];
    
    [self.contentView addSubview:self.newBtn];
    [self.newBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(5));
        make.right.mas_equalTo(MBHeightScale(-5));
        make.height.mas_equalTo(MBHeightScale(16));
        make.width.mas_equalTo(MBHeightScale(35));
    }];
    
    [self.contentView addSubview:self.timeLab];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-5));
        make.right.mas_equalTo(MBWidthScale(-10));
    }];
    
}

#pragma mark - Setter && Getter
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.clipsToBounds = YES;
    }
    return _iconImageView;
}


- (UILabel *)timeLab {
    if (!_timeLab) {
        _timeLab = [UILabel new];
        _timeLab.font = PDMediumScale(20);
        _timeLab.textColor = HEXS(@"ffffff");
        _timeLab.text = @"00:10";
        _timeLab.hidden = YES;
    }
    return _timeLab;
}

- (UIButton *)newBtn {
    if (!_newBtn) {
        _newBtn = [UIButton new];
        [_newBtn setBackgroundColor:HEXS(@"18B81E")];
        [_newBtn setTitle:@"NEW" forState:0];
        [_newBtn setTitleColor:HEXS(@"ffffff") forState:0];
        _newBtn.titleLabel.font = PDSemiBoldScale(10);
        _newBtn.layer.cornerRadius = MBHeightScale(8);
        _newBtn.userInteractionEnabled = NO;
        _newBtn.hidden = YES;
    }
    return _newBtn;
}

- (UIVisualEffectView *)effectView {
    if (!_effectView) {
        UIBlurEffect *blurEffrct = [UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark];
        _effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
//        _effectView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
        _effectView.alpha = 0.95;
        _effectView.hidden = YES;
    }
    return _effectView;
}

- (UIImageView *)starImageView {
    if (!_starImageView) {
        _starImageView = [UIImageView new];
        _starImageView.image = ImageWithName(@"home_effect_lock");
        _starImageView.hidden = YES;
    }
    return _starImageView;
}
@end
